"""
@author: 江同学呀
@file: aggs.py
@date: 2025/1/27 16:09
@desc: 
"""
from typing import Union, Dict

from espc.common.agg_common import OrderType
from espc.common.common import Number
from espc.orm.model.base.base import _Base
from espc.orm.model.mapping.field.base_field.base_field import _BaseField


class Order(_Base):
    """
    排序方式
    """
    def __init__(self, field: Union[_BaseField, str], order_type: Union[OrderType, str]):
        super().__init__()
        self.field: Union[_BaseField, str] = field
        self.order_type: OrderType = order_type
        return

    def _build(self) -> Dict:
        return {
            self.field if isinstance(self.field, str) else self.field._field_name:
                self.order_type if isinstance(self.order_type, str) else self.order_type.value
        }


class Range(_Base):
    """
    https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-aggregations-bucket-range-aggregation.html#_keyed_response_4
    """

    def __init__(self, from_: Number = None, to: Number = None, key: str = None):
        super().__init__()
        self._from: Number = from_
        self._to: Number = to
        self._key: str = key
        return

    def _build(self) -> Dict:
        _body: Dict = {}
        if self._from is not None:
            _body["from"] = self._from
        if self._to is not None:
            _body["to"] = self._to
        if self._key is not None:
            _body["key"] = self._key
        return _body


class IPRanges(Range):
    """
    IP 范围
    """

    def __init__(self, from_: str = None, to: str = None, mask: str = None):
        super().__init__()
        self._from: str = from_
        self._to: str = to
        self._mask: str = mask
        return

    def _build(self) -> Dict:
        _body: Dict = {}
        if self._from:
            _body["from"] = self._from
        if self._to:
            _body["to"] = self._to
        if self._mask:
            _body["mask"] = self._mask
        return _body




































